<template>
  <div class="wrapper">
    <header>
      <i class="fa fa-angle-left" @click="goBack"></i>
      <p>我的预约</p>
      <div></div>
    </header>
    <div class="top-ban"></div>
    <ul v-if="ordersArr.length">
      <li v-for="item in ordersArr" :key="item.orderId">
        <div class="left">
          <p>{{ item.orderDate }}</p>
          <!-- 直接绑定套餐名称字段（替换为后端真实字段，如item.name） -->
          <p>{{ item.setmeal.name }}</p>
        </div>
        <div class="right" @click="toSetmeal(item.orderId)">取消预约</div>
      </li>
    </ul>
    <div v-else class="empty">暂无预约记录</div>
    <div class="bottom-ban"></div>
  </div>
</template>

<script>
import axios from "axios";
import { getSessionStorage } from "../common.js";
import { reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import qs from 'qs';

axios.defaults.baseURL = "http://localhost:8082/tijian/";

export default {
  setup() {
    const router = useRouter();
    const state = reactive({
      ordersArr: [],
    });

    function init() {
      const user = getSessionStorage('users');
      if (!user?.userId) {
        console.error('未获取到用户ID，无法请求预约列表');
        return;
      }

      axios
        .post("OrdersController/listOrdersByUserId", qs.stringify({
          userId: user.userId,
          state: 1
        }))
        .then((response) => {

          state.ordersArr = response.data;
          console.log('后端返回的预约数据：', state.ordersArr);
        })
        .catch((error) => {
          console.error('请求预约列表失败：', error);
        });
    }

    function toSetmeal(orderId) {
      console.log(orderId);
      // const numOrderId = parseInt(orderId) ? null : parseInt(orderId);

      axios
        .post("OrdersController/removeOrders", qs.stringify({
          orderId: orderId
        }))
        .then((response) => {
          alert("删除成功!");
        })
        .catch((error) => {
          console.error('删除列表失败!', error);
        });
      router.push({ path: '/appointmentcancel', query: { orderId } });
    }

    init();

    return {
      ...toRefs(state),
      toSetmeal,
    };
  },
  methods: {
    goBack() {
      history.go(-1);
    }
  }
};
</script>



<style scoped>
/*********************** 总容器 ***********************/
.wrapper {
  width: 100%;
  height: 100%;
  background-color: #F9F9F9;
}

/*********************** header ***********************/
header {
  width: 100%;
  height: 15.7vw;
  background-color: #FFF;

  position: fixed;
  left: 0;
  top: 0;

  display: flex;
  align-items: center;
  justify-content: space-between;

  box-sizing: border-box;
  padding: 0 3.6vw;
}

header .fa {
  font-size: 8vw;
}

/*********************** footer ***********************/
footer {
  width: 100%;
  height: 14.2vw;
  box-sizing: border-box;
  border-top: solid 1px #E9E9E9;
  background-color: #FFF;

  position: fixed;
  left: 0;
  bottom: 0;
}

footer ul {
  width: 100%;
  height: 14.2vw;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

footer ul li {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  font-size: 3vw;
  color: #999;

  user-select: none;
  cursor: pointer;
}

footer ul li .fa {
  font-size: 5vw;
}

/*********************** common样式 ***********************/
.top-ban {
  width: 100%;
  height: 15.7vw;
}

.bottom-ban {
  width: 100%;
  height: 14.2vw;
}

/*********************** ul ***********************/
ul {
  width: 86vw;
  margin: 0 auto;
}

ul li {
  width: 100%;
  height: 14vw;
  border-bottom: solid 1px #EEE;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

ul li .left {
  user-select: none;
  cursor: pointer;
}

ul li .left p:first-child {
  color: #555;
  font-size: 3.3vw;
}

ul li .left p:last-child {
  color: #333;
  font-size: 4.2vw;
  font-weight: 600;
}

ul li .right {
  font-size: 4vw;
  color: #E6A23C;
}
</style>